﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Client.Platform;
using Ebada.Platform.Model;
using Ebada.Client;
using Ebada.Core;

namespace sample1 {

    public partial class UC1 : DevExpress.XtraEditors.XtraUserControl {
        private SortableSearchableBindingList<Empolyees> _BindingList = new SortableSearchableBindingList<Empolyees>();
        public UC1() {
            InitializeComponent();
            gridControl1.DataSource = _BindingList;
        }

        private void barManager1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            switch (e.Item.Name) {
                case "btAdd":
                    Add();
                    break;
                case "btEdit":
                    Edit();
                    break;
                case "btDelete":
                    Delete();
                    break;
                case "btFind":
                    Find();
                    break;
                case "btRefresh":
                    RefreshData();
                    break;
                case "btClose":
                    Close();
                    break;
            }
        }
        /// <summary>
        /// 初始表格列
        /// </summary>
        public virtual void InitColumns() {

        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        public virtual void RefreshData() {
            _BindingList.Clear();
            _BindingList.Add(MainHelper.PlatformSqlMap.GetList<Empolyees>(null));
        }
        /// <summary>
        /// 增加数据
        /// </summary>
        public virtual void Add() {
            Empolyees obj = new Empolyees();
            obj.Empolyee_ID = obj.CreateID();
            PopupFormGridEdit frm = new PopupFormGridEdit();
            frm.InitData<Empolyees>( gridControl1, gridView1, obj);
            if (frm.ShowDialog() == DialogResult.OK) {
                try {
                    MainHelper.PlatformSqlMap.Create<Empolyees>(obj);
                    _BindingList.Add(obj);
                    gridView1.SelectRow(_BindingList.Count - 1);
                } catch (Exception e) {
                    MainHelper.ShowException(e);
                }
            }            
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        public virtual void Edit() {
            Empolyees obj = gridView1.GetFocusedRow() as Empolyees;
            if (obj != null) {
                PopupFormGridEdit frm = new PopupFormGridEdit();
                Empolyees obj2 =new Empolyees();
                ConvertHelper.CopyTo<Empolyees>(obj,obj2);
                frm.InitData<Empolyees>(gridControl1, gridView1, obj2);
                if (frm.ShowDialog() == DialogResult.OK) {
                    try {
                        MainHelper.PlatformSqlMap.Update<Empolyees>(obj2);
                        ConvertHelper.CopyTo<Empolyees>(obj2, obj);
                    } catch (Exception e) {
                        MainHelper.ShowException(e);
                    }
                }
            }
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        public virtual void Find() {
            gridView1.OptionsView.ShowAutoFilterRow = !gridView1.OptionsView.ShowAutoFilterRow;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        public virtual void Delete() {
            Empolyees obj = gridView1.GetFocusedRow() as Empolyees;
            if (obj != null) {
                if (MainHelper.ShowAskMessageBox("此操作不可逆，是否确认删除当前行？") == DialogResult.OK) {
                    try {
                        MainHelper.PlatformSqlMap.Delete<Empolyees>(obj);
                        _BindingList.Remove(obj);
                    } catch (Exception e) {
                        MainHelper.ShowException(e);
                    }
                }
            }
        }
        public virtual void Close() {
            FindForm().Close();
        }
    }
}
